package cn.mw.cmdb.enums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.mw.cmdb.exception.InputRequireException;
import cn.mw.components.mongodb.exception.BusinessException;
import cn.mw.microMonitorCommon.api.scan.IPRangeParam;
import cn.mw.microMonitorCommon.api.scan.SubnetParam;
import cn.mw.microMonitorCommon.util.IpV4Util;
import cn.mw.microMonitorCommon.util.IpV6Util;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public enum AssetsDiscoverScanIpTypeEnum {
    ips, ipRange, ipSubnet;


    public static void validIps(String ips, Boolean ipv6checked, BusinessException businessException) {
        if (StrUtil.isBlank(ips)) {
            // InputRequireException
            throw new InputRequireException(new Object[]{"IP清单"});
        }
        if (StrUtil.isNotBlank(ips)) {
            log.info("传入的ips信息: {}", ips);
            String[] strings = ips.split("[,\\n]");
            for (String ip : strings) {
                log.error("待校验的IP: {}", ip);
                boolean legalIp = ipv6checked ? IpV6Util.isIP(ip) : IpV4Util.isIP(ip);
                if (!legalIp) {
                    businessException.setArgs(new String[]{ip});
                    throw businessException;
                }
            }
        }
    }

    public static void validIpRange(List<IPRangeParam> ipRange, BusinessException businessException) {
        if (CollUtil.isEmpty(ipRange)) {
            throw new InputRequireException(new Object[]{"IP范围"});
        }
        for (IPRangeParam rangeParam : ipRange) {
            if (ObjUtil.isEmpty(rangeParam) || StrUtil.isBlank(rangeParam.getStartip()) || StrUtil.isBlank(rangeParam.getEndip())) {
                throw new InputRequireException(new Object[]{"IP范围"});
            }
            String startIp = rangeParam.getStartip().trim();
            String endIp = rangeParam.getEndip().trim();
            boolean legalRange = rangeParam.isIpv6checked() ? IpV6Util.isIP(startIp) && IpV6Util.isIP(endIp) : IpV4Util.isIP(startIp) && IpV4Util.isIP(endIp);
            if (!legalRange) {
                businessException.setArgs(new String[]{startIp + " - " + endIp});
                throw businessException;
            }
        }
    }

    public static void validIpSubnet(List<SubnetParam> ipSubnet, BusinessException businessException) {
        if (CollUtil.isEmpty(ipSubnet)) {
            throw new InputRequireException(new Object[]{"IP地址段"});
        }
        for (SubnetParam subnetParam : ipSubnet) {
            if (ObjUtil.isEmpty(subnetParam) || StrUtil.isBlank(subnetParam.getSubnet())) {
                throw new InputRequireException(new Object[]{"IP地址段"});
            }
            String subnet = subnetParam.getSubnet().trim();
            // 是否为ipv6
            boolean legalSubnet = subnetParam.isIpv6checked() ? IpV6Util.isIPv6Subnet(subnet) : IpV4Util.isIPv4Subnet(subnet);
            if (!legalSubnet) {
                businessException.setArgs(new String[]{subnet});
                throw businessException;
            }
        }
    }

    public static String formatIps(String ips) {
        return ips.replaceAll(" ", "");
    }

}
